package com.oymn.myapplication.page;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.oymn.myapplication.R;
import com.oymn.myapplication.entity.Goods;
import com.oymn.myapplication.adapter.HomeAdapter;
import com.oymn.myapplication.utils.Url;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends Fragment {

    private RecyclerView mRecyclerView;
    HomeAdapter mAdapter;

    String user_phone = null;

    private String url = Url.goods_url;
    private List<Goods> goodsList = null;


    public FirstFragment(String user_phone){
        super(R.layout.fragment_first);
        this.user_phone = user_phone;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getData();
        mRecyclerView = (RecyclerView)view.findViewById(R.id.id_recyclerview);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        mRecyclerView.setAdapter(mAdapter = new HomeAdapter(getActivity(),user_phone));

    }

    public void getData(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost request = new HttpPost(url);

                //封转参数
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("action","shouye_request"));
                try {
                    request.setEntity(new UrlEncodedFormEntity(param));
                    //获取响应的结果
                    HttpResponse response = httpClient.execute(request);
                    HttpEntity entity = response.getEntity();
                    //获取响应结果的信息
                    String result = EntityUtils.toString(entity,"UTF-8");
                    //解析json，获得所有商品
                    Gson gson = new Gson();
                    Type listType = new TypeToken<List<Goods>>(){}.getType();
                    goodsList = gson.fromJson(result, listType);
                    System.out.println(goodsList);

                    mAdapter.setmDatas(goodsList);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
